package com.byt.common.view;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.byt.base.utils.DisplayUtil;
import com.byt.base.utils.onclicktwo.AntiShake;
import com.byt.common.R;

/**
 * 认证信息对话框
 */
public class AuthenticaDialog implements View.OnClickListener {

    private Dialog mDialog;
    private View dialogView;

    private TextView tv_authentica_title;
    private TextView tv_authentica_content;
    private TextView tv_go_authentica;
    private TextView tv_cancel_authentica;

    private Builder mBuilder;

    public AuthenticaDialog(Builder builder) {
        this.mBuilder = builder;
        mDialog = new Dialog(mBuilder.getContext(), R.style.MyDialogStyle);
        dialogView = View.inflate(mBuilder.getContext(), R.layout.dialog_authentica_layout, null);
        tv_authentica_title = dialogView.findViewById(R.id.tv_authentica_title);
        tv_authentica_content = dialogView.findViewById(R.id.tv_authentica_content);
        tv_go_authentica = dialogView.findViewById(R.id.tv_go_authentica);
        tv_cancel_authentica = dialogView.findViewById(R.id.tv_cancel_authentica);
        mDialog.setContentView(dialogView);
        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = DisplayUtil.getScreenWidth(mBuilder.getContext());
        lp.height = DisplayUtil.getScreenHeight(mBuilder.getContext());
        dialogWindow.setAttributes(lp);
        mDialog.setCanceledOnTouchOutside(builder.isTouchOutside());
        tv_go_authentica.setOnClickListener(this);
        tv_cancel_authentica.setOnClickListener(this);
    }

    public void show() {
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }
        if (mBuilder.getOnListener() == null) {
            return;
        }
        int id = v.getId();
        if (id == R.id.tv_go_authentica) {
            mBuilder.getOnListener().onGoAuth();
        } else if (id == R.id.tv_cancel_authentica) {
            mBuilder.getOnListener().onCancelAuth();
        }
    }

    public static class Builder {

        private OnAuthenticaListener onListener;
        private boolean isTouchOutside;
        private Context mContext;

        public Builder(Context context) {
            this.mContext = context;
            this.onListener = null;
            this.isTouchOutside = true;
        }

        public OnAuthenticaListener getOnListener() {
            return onListener;
        }

        public Builder setOnListener(OnAuthenticaListener onListener) {
            this.onListener = onListener;
            return this;
        }

        public boolean isTouchOutside() {
            return isTouchOutside;
        }

        public Builder setCanceledOnTouchOutside(boolean isTouchOutside) {
            this.isTouchOutside = isTouchOutside;
            return this;
        }

        public AuthenticaDialog build() {
            return new AuthenticaDialog(this);
        }

        public Context getContext() {
            return mContext;
        }

        public void setContext(Context context) {
            mContext = context;
        }

        public void setTouchOutside(boolean touchOutside) {
            isTouchOutside = touchOutside;
        }
    }

    public interface OnAuthenticaListener {
        /**
         * 去认证
         */
        void onGoAuth();

        /**
         * 以后再说
         */
        void onCancelAuth();
    }
}